package com.example.bishe.controller.jzg;

import com.alibaba.fastjson.JSON;
import com.example.bishe.entity.*;
import com.example.bishe.service.*;
import com.example.bishe.service.impl.JxbmServiceImpl;
import com.example.bishe.service.impl.RygzServiceImpl;
import com.example.bishe.service.impl.Zsj_jzgServiceImpl;
import com.example.bishe.service.impl.Zxbz_dwServiceImpl;
import com.example.bishe.toolclass.CUD;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author yws
 * @Date 2022/4/9 20:36
 */
@RestController
@RequestMapping("/jzg")
public class JzgController {
    @Autowired
    CUD cud;
    @Autowired
    Zsj_jzgService jzgService = new Zsj_jzgServiceImpl();
    @Autowired
    Zxbz_dwService dwService = new Zxbz_dwServiceImpl();
    @Autowired
    JxbmService jxbmService = new JxbmServiceImpl();
    @Resource
    RygzService rygzService = new RygzServiceImpl();

    @PostMapping("/addjzg")
    public String addjzg(@RequestBody Zsj_jzg jzg) {
        int errcode = 0;//错误代码1601
        String errmsg = " ";//错误描述
        Zsj_jzg zsj_jzg = new Zsj_jzg();
        zsj_jzg.setZgh(jzg.getZgh());
        Zsj_jzg[] zsj_jzgs = jzgService.select_jzg(zsj_jzg);
        if (jzg.getZgh() == null) {
            errmsg += "请求中没有添加教师的工号信息，";
        }
        if (jzg.getXm() == null) {
            errmsg += "请求中没有添加教师姓名信息，";
        }
        if (jzg.getXbdm() == 0) {
            errmsg += "请求中没有添加教师的性别信息，";
        }
        if (jzg.getKsdm() == null) {
            errmsg += "请求中没有添加教师的科室信息，";
        }
        if (jzg.getSfzjh() == null) {
            errmsg += "请求中没有添加教师的身份证信息，";
        }
        if (jzg.getSjh() != null) {
            String x = "1(?:3\\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\\d|9\\d)\\d{8}";
            if (!jzg.getSjh().matches(x))
                errmsg += "手机号格式不对，请确定你的手机号是否正确，";
        }
        if (errmsg.length() > 1) {
            errcode = 1601;
            errmsg = errmsg.substring(1, errmsg.length() - 1) + "。";
        } else if (zsj_jzgs.length == 0) {/*确保不存在这个教师的工号等信息*/
            Zxbz_dw zxbzDw = new Zxbz_dw();
            zxbzDw.setDm(jzg.getKsdm());
            Zxbz_dw[] zxbz_dws = dwService.select_dw(zxbzDw);
            if (zxbz_dws.length > 0) {
                zxbzDw = zxbz_dws[0];
                Jxbm jxbm = new Jxbm();
                jxbm.setDm(zxbzDw.getLs());
                Jxbm[] jxbms = jxbmService.selectJxbm(jxbm);
                jxbm = jxbms[0];
                jzg.setDwdm(jxbm.getLs());
                jzg.setMm(jzg.getZgh());
                jzgService.insert_jzg(jzg);
                errcode = 0;
                errmsg += "成功添加教职工信息。";
            } else {
                errcode = 1601;
                errmsg += "你所填写的单位不存在。";
            }
        } else {
            errcode = 1601;
            errmsg += "你所要加入的教师信息，工号已经存在。";
            errmsg = errmsg.substring(1, errmsg.length());
        }
        Map<String, String> map = new HashMap();
        map.put("errcode", String.valueOf(errcode));
        map.put("errmsg", errmsg);
        String s = JSON.toJSONString(map);
        return s;
    }

    @PostMapping("/deletejzg")
    public String deletejzg(@RequestBody Zsj_jzg jzg) {
        int errcode = 0;//错误代码1602
        String errmsg = "";//错误描述
        HashMap<String, String> value = new HashMap();
        if (jzg.getZgh() == null) {
            errcode = 1602;
            errmsg = "请传入你要删除的教师工号。";
        } else {
            Zsj_jzg zsjJzg = new Zsj_jzg();
            zsjJzg.setZgh(jzg.getZgh());
            Zsj_jzg[] zsjJzgs = jzgService.selectdw(zsjJzg);
            if (zsjJzgs.length != 0) {
                zsjJzg = zsjJzgs[0];
                value = cud.deletestudent_jzg(zsjJzg.getZgh());
                if (value.get("errcode").equals("0") || value.get("errcode").equals("60111")) {
                    Rygz rygz = new Rygz();
                    rygz.setUserid(jzg.getZgh());
                    rygzService.deleteRygz(rygz);
                    jzgService.delect_jzg(jzg);
                    errmsg = "已删除对应的教师信息。";
                } else {
                    errmsg += cud.deletestudent_jzgpan(value);
                    errcode = 1602;
                }
            } else {
                errcode = 1602;
                errmsg = "没找到你想删除的教职工信息。";
            }

        }
        Map map = new HashMap();
        map.put("errcode", String.valueOf(errcode));
        map.put("errmsg", errmsg);
        String s = JSON.toJSONString(map);
        return s;
    }

    @PostMapping("/updatejzg")
    public String updatejzg(@RequestBody Zsj_jzg jzg) {
        int errcode = 0;//错误代码1703
        String errmsg = "";//错误描述
        HashMap<String, String> value = new HashMap();
        if(jzg.getSjh()!=null){
            String x = "1(?:3\\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\\d|9\\d)\\d{8}";
            if(!jzg.getSjh().matches(x)){
                errcode=1603;
                errmsg+="手机号格式不对，请确定你的手机号是否正确。";
            }
        }
        if (jzg.getZgh() == null) {
            errcode = 1603;
            errmsg = "请最少传入你要修改的教师的工号。";
        } else if(errcode==0){
            Zsj_jzg zsjJzg = new Zsj_jzg();
            zsjJzg.setZgh(jzg.getZgh());
            Zsj_jzg[] zsjJzgs = jzgService.selectdw(zsjJzg);
            if (zsjJzgs.length != 0) {
                zsjJzg = zsjJzgs[0];
                String wx_f = "";
                if (jzg.getKsdm() != null) {
                    Zxbz_dw dw = new Zxbz_dw();
                    dw.setDm(jzg.getKsdm());
                    Zxbz_dw[] zxbz_dws = dwService.select_dw(dw);
                    if (zxbz_dws.length != 0) {
                        dw = zxbz_dws[0];
                        wx_f = zxbz_dws[0].getWx_id() + "";
                        value = cud.updatestudent_jzg(zsjJzg.getZgh(),
                                jzg.getXm(),
                                jzg.getXbdm() + "",
                                jzg.getEmail(),
                                wx_f, jzg.getSjh());
                        if (value.get("errcode").equals("0") || value.get("errcode").equals("60111")) {
                            errcode = 0;
                            errmsg += "已修改对应教师的信息";
                            Jxbm jxbm = new Jxbm();
                            jxbm.setDm(dw.getLs());
                            Jxbm[] jxbms = jxbmService.selectJxbm(jxbm);
                            jxbm = jxbms[0];
                            jzg.setDwdm(jxbm.getLs());
                            jzgService.query_jzg(jzg);
                            Rygz rygz = new Rygz();
                            rygz.setUserid(jzg.getZgh());
                            rygz.setUsername(jzg.getXm());
                            rygz.setPhone(jzg.getSjh());
                            rygzService.queryRygz(rygz);
                        } else {
                            errcode = 1603;
                            errmsg += cud.updatestudent_jzgpan(value);
                        }
                    } else {
                        errcode = 1603;
                        errmsg = "输入的科室代码编号不存在。";
                    }
                } else {
                    cud.updatestudent_jzg(zsjJzg.getZgh(), jzg.getXm(),
                            jzg.getXbdm() + "",
                            jzg.getEmail(),
                            null, jzg.getSjh());
                    jzg.setDwdm(zsjJzg.getDwdm());
                    jzgService.query_jzg(jzg);
                }
            } else {
                errcode = 1603;
                errmsg = "没找到你想要修改的教职工信息。";
            }
        }
        Map map = new HashMap();
        map.put("errcode", String.valueOf(errcode));
        map.put("errmsg", errmsg);
        String s = JSON.toJSONString(map);
        return s;
    }

    @PostMapping("/selectjzg")
    public String selectjzg(@RequestBody Zsj_jzg jzg) {
        Zsj_jzg[] zsj_jzgs = jzgService.select_jzg(jzg);
        List<Zsj_jzg> list = new ArrayList<>();
        for (int i = 0; i < zsj_jzgs.length; i++) {
            list.add(zsj_jzgs[i]);
        }
        Map map = new HashMap();
        map.put("Zsj_jzg", list);
        String s = JSON.toJSONString(map);
        System.out.println(s);
        return s;
    }
}
